import { create } from 'zustand'

interface User {
  id: string
  name: string
  phone: string
  role: 'parent' | 'child' | 'admin'
  profile_data?: {
    children?: Array<{
      id: string
      name: string
      age: number
      interests: string[]
    }>
  }
}

interface AuthState {
  user: User | null
  isAuthenticated: boolean
  isLoading: boolean
  login: (phone: string, password: string) => Promise<void>
  logout: () => void
  setUser: (user: User | null) => void
}

export const useAuthStore = create<AuthState>((set) => ({
  user: null,
  isAuthenticated: false,
  isLoading: false,
  
  login: async (phone: string, password: string) => {
    set({ isLoading: true })
    try {
      // TODO: 集成Supabase认证
      const mockUser: User = {
        id: '1',
        name: '张妈妈',
        phone,
        role: 'parent',
        profile_data: {
          children: [
            { id: '1', name: '小明', age: 5, interests: ['动物', '科学'] }
          ]
        }
      }
      set({ user: mockUser, isAuthenticated: true })
    } catch (error) {
      throw error
    } finally {
      set({ isLoading: false })
    }
  },
  
  logout: () => {
    set({ user: null, isAuthenticated: false })
  },
  
  setUser: (user) => {
    set({ user, isAuthenticated: !!user })
  }
}))